home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 016 / source.files / ealstartup.asm < prev    next >
Assembly Source File  |  1995-03-17  |  7KB  |  260 lines

  1.    section   text,CODE
  2. * C initial startup procedure under AmigaDOS
  3. * Requirements:
  4.  
  5.    INCLUDE   "exec/types.i"
  6.    INCLUDE "exec/alerts.i"
  7.    INCLUDE "exec/nodes.i"
  8.    INCLUDE "exec/lists.i"
  9.    INCLUDE "exec/ports.i"
  10.    INCLUDE "exec/libraries.i"
  11.    INCLUDE "exec/tasks.i"
  12.    INCLUDE "libraries/dos.i"
  13.    INCLUDE "libraries/dosextens.i"
  14.    INCLUDE "workbench/startup.i"
  15.  
  16. ; some usefull macros:
  17. xlib   macro
  18.    xref   _LVO\1
  19.    endm
  20.  
  21. callsys   macro
  22.    CALLLIB   _LVO\1
  23.    endm
  24.    
  25.  
  26.    xdef   _XCEXIT         * exit(code) is standard way to leave C.
  27.  
  28.    xref   _Debug
  29.    xref   __main         * Name of C program to start with.
  30.    xref   _MemCleanup
  31.    xref   _AbsExecBase
  32.    xref   _FindTask
  33.    xref   _main         * added by robp per EA instructions
  34.    xlib   Alert
  35.    xlib   FindTask
  36.    xlib   Forbid
  37.    xlib   GetMsg
  38.    xlib   OpenLibrary
  39.    xlib   ReplyMsg
  40.    xlib   Wait
  41.    xlib   WaitPort
  42.    xlib   Open
  43.    xlib   Close
  44.    xlib   CurrentDir
  45.  
  46. start:
  47.    move.l   d0,dosCmdLen
  48.    move.l   a0,dosCmdBuf
  49.    move.l   a7,d0         ; save old stack ptr
  50.    movem.l   d1-d6/a0-a6,-(a7)
  51.    move.l   d0,a5
  52.    move.l   _AbsExecBase,a6
  53.    move.l   a6,_SysBase
  54.    move.l   a7,__StackPtr      * Save stack ptr
  55.    clr.l   _WBenchMsg
  56.  
  57. ;------ get the address of our task
  58.    suba.l   a1,a1
  59.    callsys   FindTask
  60.    move.l   d0,a4
  61.  
  62. ;------ are we running as a son of Workbench?
  63.    move.l   pr_CurrentDir(A4),_curdir
  64.    tst.l   pr_CLI(A4)
  65.    beq   fromWorkbench
  66.  
  67. ;=======================================================================
  68. ;====== CLI Startup Code ===============================================
  69. ;=======================================================================
  70.  
  71. fromCLI:
  72.    move.l   a5,D0      ; get top of stack
  73.    sub.l   4(a5),D0   ; compute bottom 
  74.    move.l   D0,__base   ; save for stack checking
  75. ;------   attempt to open DOS library:
  76.    bsr   openDOS
  77.  
  78. ;------ find command name:
  79.    move.l   #start,a0
  80. ;   jsr   _Debug
  81.    clr.l   -(sp)
  82.    jsr   _FindTask
  83.    addq.l   #4,sp
  84.    move.l   d0,a0
  85.    move.l   pr_CLI(a0),a0
  86.    add.l   a0,a0      ; bcpl pointer conversion
  87.    add.l   a0,a0
  88.    move.l   cli_CommandName(a0),a1
  89.    add.l   a1,a1      ; bcpl pointer conversion
  90.    add.l   a1,a1
  91.  
  92. ;------   collect parameters:
  93.    move.l   dosCmdLen,d0      ; get command line length
  94.    moveq.l   #0,d1
  95.    move.b   (a1)+,d1
  96.    move.l   a1,__ProgramName
  97.    add.l   d1,d0         ; add length of command name
  98.    addq.l   #1,d0         ; allow for space after command   
  99.  
  100.    clr.w   -(A7)         ; set null terminator for command line
  101.    addq.l   #1,D0         ; force to even number of bytes
  102.    andi.w   #$fffe,D0      ;(round up)
  103.    sub.l   D0,A7         ; make room on stack for command line
  104.    subq.l   #2,D0
  105.    clr.w   0(A7,D0)
  106.  
  107. ;------ copy command line onto stack
  108.    move.l   dosCmdLen,d0      ; get command line length
  109.    move.l   dosCmdBuf,a0
  110.    move.l   d0,d2
  111.    subq.l   #1,d0
  112.    add.l   d1,d2
  113.  
  114. copy_line:
  115.    move.b   0(A0,D0.W),0(A7,D2.W)   ; copy command line to stack
  116.    subq.l   #1,d2
  117.    dbf   d0,copy_line
  118.    move.b   #' ',0(a7,d2.w)      ; add space between command and parms
  119.    subq.l   #1,d2
  120.  
  121. copy_cmd:
  122.    move.b   0(a1,d2.w),0(a7,d2.w)   ; copy command name to stack
  123.    dbf   d2,copy_cmd
  124.    move.l   A7,A1
  125.    move.l   A1,-(A7)      ; push command line address
  126.    jsr   __main              * call C entrypoint
  127.    moveq.l   #0,d0         ; set successful status
  128.    bra.s   exit2
  129.  
  130. ;=======================================================================
  131. ;====== Workbench Startup Code =========================================
  132. ;=======================================================================
  133.  
  134. fromWorkbench:
  135.  
  136.    move.l   TC_SPLOWER(a4),__base   ; set base of stack
  137. ;------ open the DOS library:
  138.    bsr   openDOS
  139.  
  140. ;------ we are now set up.  wait for a message from our starter
  141.    bsr   waitmsg
  142.    move.l   d0,_WBenchMsg
  143.    move.l   d0,-(SP)
  144. ;
  145.    move.l   d0,a2         ; get first argument
  146.    move.l   sm_ArgList(a2),d0
  147.    beq.s   do_cons
  148.    move.l   _DOSBase,a6
  149.    move.l   d0,a0
  150.    move.l   wa_Lock(a0),d1
  151.    callsys   CurrentDir
  152. do_cons:
  153.    move.l   sm_ToolWindow(a2),d1   ; get the window argument
  154.    beq.s   do_main
  155.    move.l   #MODE_OLDFILE,d2
  156.    callsys   Open
  157.    move.l   d0,stdin
  158.    beq.s   do_main
  159.    lsl.l   #2,d0
  160.    move.l   d0,a0
  161.    move.l   fh_Type(a0),pr_ConsoleTask(A4)
  162. do_main:
  163.    clr.l   -(sp)      *was pea _NULL
  164.    jsr   _main              * call C entrypoint (was jsr __main)
  165.    moveq.l   #0,d0         ; set successful status
  166.    bra.s   exit2
  167. ;
  168.  
  169. _XCEXIT:
  170.    move.l   4(SP),d0   ; extract return code
  171. exit2:
  172.    move.l   d0,-(a7)
  173.    jsr   _MemCleanup      ; cleanup leftover memory alloc.
  174.  
  175. ;------ if we ran from CLI, skip workbench cleanup:
  176.    tst.l   _WBenchMsg
  177.    beq.s   exitToDOS
  178.    move.l   _console_dev,d1
  179.    beq.s   done_2
  180.    callsys   Close
  181. done_2:
  182.    move.l   stdin,d1
  183.    beq.s   done_4
  184.    callsys   Close
  185. done_4:
  186.  
  187. ;------ return the startup message to our parent
  188. ;   we forbid so workbench can't UnLoadSeg() us
  189. ;   before we are done:
  190.    move.l   _AbsExecBase,A6
  191.    callsys Forbid
  192.    move.l   _WBenchMsg,a1
  193.    callsys   ReplyMsg
  194.  
  195. ;------ this rts sends us back to DOS:
  196. exitToDOS:
  197.    MOVE.L   (A7)+,D0
  198.    movea.l  __StackPtr,SP      * restore stack ptr
  199.    movem.l   (a7)+,d1-d6/a0-a6
  200.    rts            * and exit
  201.  
  202. ;-----------------------------------------------------------------------
  203. noDOS:
  204.       ALERT   (AG_OpenLib!AO_DOSLib)
  205.       moveq.l   #100,d0
  206.       bra.s   exit2
  207.  
  208.  
  209. ;-----------------------------------------------------------------------
  210. ; This routine gets the message that workbench will send to us
  211. ; called with task id in A4
  212.  
  213. waitmsg:
  214.       lea   pr_MsgPort(A4),a0     * our process base
  215.       callsys   WaitPort
  216.       lea   pr_MsgPort(A4),a0     * our process base
  217.       callsys GetMsg
  218.       rts
  219.  
  220. ;-----------------------------------------------------------------------
  221. ;  Open the DOS library:
  222.  
  223. openDOS
  224.       lea   DOSName,A1
  225.       moveq.l   #0,D0
  226.       callsys OpenLibrary
  227.       move.l   D0,_DOSBase
  228.       beq   noDOS
  229.       rts
  230.  
  231.    section   data,DATA
  232. ;
  233.    XDEF   _NULL,_DOSBase,_SysBase,_LoadAddress,_console_dev,_WBenchMsg
  234.    XDEF   __mbase,__mnext,__msize,__tsize,__oserr,__fperr,__mstep
  235.    XDEF   __ProgramName,__StackPtr,__base
  236. ;
  237. _NULL   DC.L   0         ;
  238. __base   DC.L   0         ; base of stack
  239. __mbase   DC.L   0         ; base of memory pool
  240. __mnext   DC.L   0         ; next available memory location
  241. __msize   DC.L   0         ; size of memory pool
  242. __mstep   DC.L   1024         ; memory pool step size
  243. __tsize   DC.L   0         ; total size?
  244. __oserr   DC.L   0
  245. __fperr   DC.L   0
  246. _curdir   DC.L   0
  247. _console_dev DC.L 0
  248. _SysBase     DC.L 0
  249. _DOSBase     DC.L 0   
  250. _LoadAddress DC.L 0         ; program load address
  251. _WBenchMsg   dc.l   0
  252. __StackPtr   dc.l   0
  253. dosCmdLen   dc.l   0
  254. dosCmdBuf   dc.l   0
  255. stdin   dc.l   0
  256. __ProgramName   dc.l   0
  257. DOSName    DOSNAME
  258.  
  259.